export default class Sprite3DMoveContorller {
    static frompostion: Laya.Vector3;
    static topostion: Laya.Vector3;
    static moveDistance: Laya.Vector3;
    static moveObjcet: Laya.Sprite3D;
    static moveTime: number = 0;
    static moveValue: number = 0.01;
    static callback: Function;
    static obj: any;

    /**
     * Sprite3D移动方法
     * @param gameObject 需要移动的3D对象
     * @param toPos 移动目标点
     * @param playTime 移动总耗时
     * @param easeFun 缓动函数 (Laya.Ease) 默认为线性移动
     * @param obj 回调域
     * @param callback 回调函数 
     */
    public static onPositionMoveTo(gameObject: Laya.Sprite3D, toPos: Laya.Vector3, playValue: number, easeFun: Function, obj: any = null, callback: Function = null) {
        this.moveDistance = new Laya.Vector3(toPos.x - gameObject.transform.position.x, toPos.y - gameObject.transform.position.y, toPos.z - gameObject.transform.position.z);
        this.frompostion = gameObject.transform.position;
        this.topostion = toPos;
        this.moveObjcet = gameObject;
        this.moveValue = playValue;
        this.callback = callback;
        this.obj = obj;
        this.startMove();
    }

    private static startMove() {
        Laya.timer.frameLoop(1, this, this.moveUpdate);
    }

    private static endMove(handler: Laya.Handler) {
        Laya.timer.clear(this, this.moveUpdate);
        if (handler != null) {
            handler.run();
        }
    }

    private static moveUpdate() {
        let a = Laya.Vector3.distance(this.moveObjcet.transform.position, this.topostion);
        if (a < 0.1) {
            this.endMove(Laya.Handler.create(this.obj, this.callback));
        } else {
            this.moveObjcet.transform.position = new Laya.Vector3(
                this.frompostion.x + (this.moveDistance.x * this.moveValue),
                this.frompostion.y + (this.moveDistance.y * this.moveValue),
                this.frompostion.z + (this.moveDistance.z * this.moveValue)
            );
        }
    }
}